import ChannelBase from "../base/ChannelBase";
import ChannelData from "../data/ChannelData";

export default class WeiXinChannel extends ChannelBase {

    /**
     * 初始化渠道
     */
    protected InitChannel() {
        this.is_has_ad = true;
        this.is_has_box = true;
        this.is_has_native = true;
        this.banner_height = 280;
        wx.showShareMenu({ withShareTicket: true });
        wx.onShareAppMessage(() => {
            return {
                title: ChannelData.share_title,
                imageUrl: ChannelData.share_image_url
            }
        })

        if (this.is_has_ad) {
            this.CreateVideo();
            this.CreatePage();
        }
    }

    //#region 广告相关
    /**
     * 创建插屏广告
     */
    public CreatePage() {
        let self = this;
        this.page_obj = wx.createInterstitialAd({
            adUnitId: self.page_id_list[0]
        });
        this.page_obj.load();
        this.page_obj.onLoad(function () {
            console.log("load page successed!");
        });
        this.page_obj.onError(function (res) {
            console.error(res.errMsg + '----' + res.errCode);
        });
    }
    /**
     * 展示插屏广告
     */
    public ShowPage(on_successed: Function, on_failed: Function) {
        let self = this;
        this.page_obj.show().catch((res) => {
            console.error(res.errMsg + '----' + res.errCode);
        })
    }

    /**
     * 创建视频广告
     */
    public CreateVideo() {
        let self = this;
        this.video_obj = wx.createRewardedVideoAd({
            adUnitId: self.video_id_list[0]
        });
        this.video_obj.load();
        this.video_obj.onClose(res => {
            if (res.isEnded || res === undefined) {
                if (self.video_successed != null && self.video_successed != undefined) {
                    if (self.video_successed) self.video_successed();
                }
            } else {
                if (self.video_failed != null && self.video_failed != undefined) {
                    if (self.video_failed) self.video_failed();
                }
            }
        });
        this.video_obj.onError(res => {
            if (self.video_failed != null && self.video_failed != undefined) {
                if (self.video_failed) self.video_failed();
            }
            console.error(res.errMsg + '----' + res.errCode);
        });
    }
    /**
     * 展示视频广告
     */
    public ShowVideo(on_successed: Function, on_failed: Function) {
        let self = this;
        this.video_successed = on_successed;
        this.video_failed = on_failed;
        this.video_obj.show().catch(err => {
            console.log('show video failed!!!');
            if (self.video_failed) self.video_failed();
        });
    }

    /**
     * 展示banner广告
     */
    public ShowBanner(on_successed: Function, on_failed: Function) {
        if (this.banner_obj == null || this.banner_obj == undefined) {
            let sysInfo = wx.getSystemInfoSync();
            let windowW = sysInfo.windowWidth;
            let windowH = sysInfo.windowHeight;
            let self = this;
            this.banner_obj = wx.createBannerAd({
                adUnitId: self.banner_id_list[0],
                style: {
                    left: 3000,
                    top: 5000,
                    width: 300,
                }
            });
            this.banner_obj.onLoad(() => {
                if (self.banner_obj != null) {
                    self.banner_obj.show();
                }
                else console.warn("banner is null!");
            });

            this.banner_obj.onError(res => {
                if (on_failed) on_failed();
                console.error(res.errMsg + '----' + res.errCode);
            });

            this.banner_obj.onResize(function (res) {
                self.banner_obj.style.top = windowH - res.height;
                self.banner_obj.style.left = (windowW - res.width) / 2;
                self.banner_height = res.height;
                if (on_successed) on_successed();
            });

            this.banner_failed = on_failed;
        }
    }
    /**
     * 隐藏banner广告
     */
    public HideBanner() {
        if (this.banner_obj != null && this.banner_obj != undefined) {
            this.banner_obj.destroy();
            this.banner_obj = null;
            if (this.banner_failed) {
                try {
                    this.banner_failed();
                } catch (error) {

                }
            }
        }
    }

    /**
     * 创建盒子广告
     */
    public CreateBox() { }
    /**
     * 显示盒子广告
     */
    public ShowBox(on_successed: Function, on_failed: Function) {
        if (this.box_obj) return;
        let self = this;
        let sysInfo = wx.getSystemInfoSync();
        let windowW = sysInfo.windowWidth;
        let windowH = sysInfo.windowHeight;
        this.box_obj = wx.createGridAd({
            adUnitId: this.box_id_list[0],
            style: {
                left: 5000,
                top: 5000,
                width: windowW * 0.3,
                height: windowH * 0.3
            },
            adTheme: 'white',
            gridCount: 8
        });

        this.box_obj.onLoad(function () {
            if (self.box_obj != null) {
                self.box_obj.show();
                self.HideBanner();
            }
        })

        this.box_obj.onResize(function (res) {
            self.box_obj.style.top = windowH - res.height * 1.5;
            self.box_obj.style.left = (windowW - res.width) / 2;
            if (on_successed) on_successed();
        });

        self.box_obj.onError(res => {
            self.box_obj = null;
            if (on_failed) on_failed();
        });
    }
    /**
     * 隐藏盒子广告
     */
    public HideBox() {
        if (this.box_obj != null && this.box_obj != undefined) {
            this.box_obj.destroy();
            this.box_obj = null;
        }
    }

    /**
     * 创建积木广告
     */
    public CreateJuggle() { }
    /**
     * 显示积木广告
     */
    public ShowJuggle() { }
    /**
     * 隐藏积木广告
     */
    public HideJuggle() { }

    /**
     * 创建原生广告
     */
    public CreateNative() { }
    /**
    * 展示原生广告
    */
    public ShowNative(on_successed: Function, on_failed: Function) {
        let self = this;
        this.native_obj = wx.createCustomAd({
            adUnitId: self.native_id_list[0],
            left: 10,
            top: 10,
            fixed: false
        })
        this.native_obj.show().catch((res) => {
            if (on_failed) on_failed();
            console.error(res.errMsg + '----' + res.errCode);
        });


    }
    /**
    * 隐藏原生广告
    */
    public HideNative() {
        if (this.native_obj != null && this.native_obj != undefined) {
            this.native_obj.destroy();
            this.native_obj = null;
        }
    }
    //#endregion 广告相关

    //#region 分享/录屏相关 
    /**
     * 分享游戏
     */
    public ShareGame() {
        wx.shareAppMessage({
            title: ChannelData.share_title,
            imageUrl: ChannelData.share_image_url
        })
    }

    /**
     * 开始录屏
     */
    public StartRecord() { }
    /**
     * 停止录屏
     */
    public StopRecord(on_stop: Function) { }
    /**
     * 分享录屏
     */
    public ShareRecord(on_successed: Function, on_failed: Function) { }
    //#endregion 分享/录屏相关

    //#region 设备相关
    /*
     * 获取设备信息
     */
    public GetSystemInfo() {
        return wx.getSystemInfoSync();
    }
    /*
     * 短震动
     */
    public VibrateShort() {
        wx.vibrateShort({});
    }

    /*
     * 长震动
     */
    public VibrateLong() {
        wx.vibrateLong({});
    }
    //#endregion

    //#region 开放功能
    /**
     * 打开另一个小程序
     * @param target 目标小程序appid
     * @param path 打开的页面路径
     * @param extraData 需要传递给目标小程序的数据
     * @param ON_SUCCESS 成功回调,返回参数res
     * @param ON_FAILED 失败回调,返回参数res
     * @param is_developer envVersion 的合法值是否为 develop(开发版)
     */
    public NavigateToMiniProgram(target: string, path: string, extraData: object, ON_SUCCESS: Function, ON_FAILED: Function, is_developer = false) {
        wx.navigateToMiniProgram({
            appId: target,
            path: path,
            extraData: extraData,
            envVersion: is_developer ? 'develop' : 'release',
            ON_SUCCESS,
            ON_FAILED
        })
    }
    //#endregion

    //#region 生命周期
    public OnShow(callback: Function) {
        wx.onShow(callback);
    }
    public OnHide(callback: Function) {
        wx.onHide(callback);
    }
    public OffShow(callback: Function) {
        wx.offShow(callback);
    }
    public OffHide(callback: Function) {
        wx.offHide(callback);
    }
    public GetLaunchOptionsSync(): any {
        return wx.getLaunchOptionsSync();
    }
    public GetEnterOptionsSync(): any {
        return wx.getEnterOptionsSync();
    }
    public ExitMiniProgram(on_successed: Function, on_failed: Function, on_complete: Function) {
        wx.exitMiniProgram(on_successed, on_failed, on_complete);
    }
    //#endregion 生命周期
}
